<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @package   App_Sample
 * @author    Cheng Wei <berlios.o@gmail.com>
 * @version   $$Id: GetSortClass.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * Get Revage rate and get rate count
 * 
 * @package   App_Sample
 * @author    Chang ZhenHua <chang.zhenhua@gmail.com>
 * @version   $$Id: GetSortClass.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
class Evebit_View_Helper_GetRate extends Zend_View_Helper_Abstract {
	protected $firstload = false;	
	
    function getRate($ratetype=null,$type=null,$info_id=null,$user_id=null,$comment_id=null) {
    	//load script
    	if (!$this->firstload) {
    		//load script start
    		$this->view->headScript()->prependFile($this->view->baseUrl("media/js/jquery.raty.min.js"));
    		$this->view->headScript()->captureStart();
    		echo "$().ready(function() {
	                  $('.star_container').each(function() {
		                  var score = $(this).attr('title');
		                  if (score == -1) {
		                      return true;
		                  }
		                  $('#'+this.id).raty({
	                          path:  '".$this->view->baseUrl('media/images')."',
	                          start:    score
                          })
	                  });
                   });
                   $().ready(function() {
	                  $('.star_container_readonly').each(function() {
		                  var score = $(this).attr('title');
		                  if (score == -1) {
		                      return true;
		                  }
		                  $('#'+this.id).raty({
		                      readOnly:  true,
	                          path:  '".$this->view->baseUrl('media/images')."',
	                          start:    score
                          })
	                  });
                   });
                   ";
    		$this->view->headScript()->captureEnd();
    		//load script end
    		$this->firstload = true;
    	}
    	
		if ($ratetype == 'default') {
			return 5;
		} else if ($ratetype == 'average') {
			$comment = new Default_Model_Entity_Comment();
			$rates=$comment->getAvgRate($type,$info_id);
			foreach ($rates as $rate) {
				if($rate->getInfoId()==$info_id){
					$rateScore=$rate->getRatecore();
					return floor($rate->getRatecore());
				}
			}
			return 0;
		} else if ($ratetype == 'userrate') {
			$comment = new Default_Model_Entity_Comment();
			$item=$comment->getUserRate($user_id,$type,$info_id,$comment_id);
			if (!$item) {
				return -1;
			} else {
				return floor($item['rate']);
			}
		}else {
			return 0;
		}
	}
	
}